package com.example.reference_book.controller;

import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.Course;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("SearchBookUtil")
public class SearchBookUtil {

    @Resource
    private searchBookNameStrategy searchName;

    @Resource
    private searchBookTagStrategy searchTag;

    @Resource
    private BookStrategy bookStrategy;

    @Resource
    private Context context;

    public static SearchBookUtil searchBookUtil=new SearchBookUtil();

    public static SearchBookUtil getSearchBookUtil() {
        return searchBookUtil;
    }

    @GetMapping("/{type}/{key}")
    public List<Book> search(@PathVariable("type") String type, @PathVariable("key") String key){
        System.out.println("type: "+type);
        if(type.equals("名称")){
            System.out.println("name");
            this.bookStrategy = this.searchName;
        }else{
            this.bookStrategy = this.searchTag;
            System.out.println("tag");
        }
        this.context.setBookStrategy(this.bookStrategy);
        return this.context.executeStrategy(type,key);
    }
}

